package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.ruoyi.system.dl.DeleteImages;
import com.ruoyi.system.domain.Images;
import com.ruoyi.system.mapper.ImagesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TravelnotesMapper;
import com.ruoyi.system.domain.Travelnotes;
import com.ruoyi.system.service.ITravelnotesService;

/**
 * 游记Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-10
 */
@Service
public class TravelnotesServiceImpl implements ITravelnotesService 
{
    @Autowired
    private TravelnotesMapper travelnotesMapper;

    @Autowired
    private ImagesMapper imagesMapper;
    /**
     * 查询游记
     * 
     * @param id 游记主键
     * @return 游记
     */
    @Override
    public Travelnotes selectTravelnotesById(Long id) {
        Travelnotes travelnotes = travelnotesMapper.selectTravelnotesById(id);
        travelnotes.setTravelNotesImage(imagesMapper.selectImages("travelNotes"+id));
        return travelnotes;
    }

    /**
     * 查询游记列表
     * 
     * @param travelnotes 游记
     * @return 游记
     */
    @Override
    public List<Travelnotes> selectTravelnotesList(Travelnotes travelnotes)
    {
        List<Travelnotes> travelnotes1 = travelnotesMapper.selectTravelnotesList(travelnotes);
        return travelnotes1.stream().peek(tra ->{
            tra.setTravelNotesImage(imagesMapper.selectImages("travelNotes"+tra.getId()));
        }).collect(Collectors.toList());
    }

    /**
     * 新增游记
     * 
     * @param travelnotes 游记
     * @return 结果
     */
    @Override
    public int insertTravelnotes(Travelnotes travelnotes) {
        int i = travelnotesMapper.insertTravelnotes(travelnotes);
        List<Travelnotes> travelnotes1 = travelnotesMapper.selectTravelnotesList(travelnotes);
        Long id = travelnotes1.get(0).getId();

        List<String> travelNotesImage = travelnotes.getTravelNotesImage();

        travelNotesImage.forEach(image -> imagesMapper.insertSelective(new Images(null,"travelNotes"+id,image)));

        return i;
    }

    /**
     * 修改游记
     * 
     * @param travelnotes 游记
     * @return 结果
     */
    @Override
    public int updateTravelnotes(Travelnotes travelnotes) {
        int i = travelnotesMapper.updateTravelnotes(travelnotes);
        String foreignId = "travelNotes" + travelnotes.getId();

        // 将所有与该id的图片删除
        imagesMapper.deleteByForeginId(foreignId);

        DeleteImages.deleteIma(foreignId,imagesMapper);

        Set<String> strings = new HashSet<>(travelnotes.getTravelNotesImage());

        // 添加
        strings.forEach(o -> imagesMapper.insert(new Images(null,foreignId,o)));

        return i;
    }

    /**
     * 批量删除游记
     * 
     * @param ids 需要删除的游记主键
     * @return 结果
     */
    @Override
    public int deleteTravelnotesByIds(Long[] ids) {
        Arrays.stream(ids).forEach(o -> {
            imagesMapper.deleteByForeginId("travelNotes"+o);
            DeleteImages.deleteIma("travelNotes"+o,imagesMapper);
        });
        return travelnotesMapper.deleteTravelnotesByIds(ids);
    }

    /**
     * 删除游记信息
     * 
     * @param id 游记主键
     * @return 结果
     */
    @Override
    public int deleteTravelnotesById(Long id) {
        String foreignId = "travelNotes"+id;
        DeleteImages.deleteIma(foreignId,imagesMapper);
        imagesMapper.deleteByForeginId(foreignId);
        return travelnotesMapper.deleteTravelnotesById(id);
    }
}
